package XishiBackend.controllers.microsite;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import XishiBackend.base.Json;
import XishiBackend.models.entity.Restaurant;
import XishiBackend.services.microsite.MicrositeRestaurantService;

@RestController
@EnableAutoConfiguration
@RequestMapping("/microsite/restaurant")
public class MicrositeRestaurantController {
    @Autowired
    private MicrositeRestaurantService rS;
    
    @RequestMapping(value = "/status",method=RequestMethod.POST)
    public String status(@RequestParam("sig") String sig,@RequestParam("restaurantId") long restaurantId) {
        Restaurant restaurant = rS.findOne(restaurantId);
        if(restaurant==null){
            return Json.format(1, "获取店铺状态失败：店铺不存在");
        }
        int startTime = restaurant.getOpenTime(),
            endTime = restaurant.getCloseTime();
        
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        //获取当前时间为当天的第几分钟
        int currentTime = calendar.get(Calendar.HOUR_OF_DAY)*60+calendar.get(Calendar.MINUTE);
        
        boolean isOpen = (currentTime >= startTime && currentTime < endTime);
        
        return Json.format(0,"获取店铺状态成功", isOpen);
    }
}
